public class Main {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = new int[]{1,7,3,6,5,6};
        System.out.println(solution.pivotIndex(nums));
    }
}
class Solution {
    public int pivotIndex(int[] nums) {
        int[] fdp = new int[nums.length];
        int[] bdp = new int[nums.length];
        //构造前缀和数组i之前
        for (int i = 0;i < nums.length;i++) {
            if (i == 0) fdp[0] = 0;
            else fdp[i] = fdp[i - 1] + nums[i -1];
        }
        //构造后缀和数组i之后
        for (int j = nums.length-1;j >= 0;j--) {
            if (j == nums.length-1) bdp[j] = 0;
            else bdp[j] = bdp[j+1] + nums[j+1];
        }
        //使用前后缀数组和
        for (int i = 0;i < nums.length;i++) {
            if (fdp[i] == bdp[i]) return i;
        }
        return -1;
    }
}